package cn.yotta.user.core.web.autoconfiguration;

import cn.yotta.user.core.server.service.LoginService;
import cn.yotta.user.core.web.component.LocalUserReadFromTokenProvider;
import cn.yotta.user.core.web.iface.TokenStorageStrategy;
import cn.yotta.user.core.web.iface.impl.CookieTokenStorageStrategy;
import cn.yotta.user.core.web.properties.LoginTokenProperties;
import cn.yotta.user.web.autoconfiguration.provider.UserReadFromTokenProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author yotta
 * @date 2023/4/11 14:19
 */
@Configuration
@ComponentScan("cn.yotta.user.core.web")
@EnableConfigurationProperties({LoginTokenProperties.class})
public class YottaUserWebAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public UserReadFromTokenProvider userReadFromTokenProvider(LoginService loginService){
       return new LocalUserReadFromTokenProvider(loginService);
    }

    @Bean
    @ConditionalOnMissingBean(TokenStorageStrategy.class)
    public TokenStorageStrategy tokenStorageStrategy(LoginTokenProperties loginProperties) {
        return new CookieTokenStorageStrategy(loginProperties);
    }
}
